<template>
  <div class="loginBody">
    <div class="loginDiv">
      <div class="login-content">
        <h1 class="login-title">用户登录</h1>
        <el-form :model="loginForm" label-width="180px" :rules="rules" ref="loginForm">
          <el-form-item label="账号" prop="no">
            <el-input
                type="text"
                v-model="loginForm.no"
                autocomplete="off"
                size="small"
                @keyup.enter.native="confirm"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
                type="password"
                v-model="loginForm.password"
                autocomplete="off"
                size="small"
                @keyup.enter.native="confirm"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
                type="primary"
                @click="confirm"
                :disabled="confirm_disabled"
            >
              确定
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "LoginVue",
  data() {
    return {
      confirm_disabled: false, // 按钮禁用状态
      loginForm: {
        no: '', // 用户账号
        password: '' // 用户密码
      },
      rules: {
        no: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    confirm() {
      this.confirm_disabled = true; // 禁用按钮
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$axios.post(this.$httpUrl + "/user/login", this.loginForm).then((res) => {
            if (res.data.code === 200) {
              sessionStorage.setItem("CurUser",JSON.stringify(res.data.data.user))
              console.log(res.data.data.menu);
              this.$store.commit("setMenu",res.data.data.menu)
              this.$router.replace('/IndexPage');
            } else {
              this.$message.error('校验失败，用户名或密码错误！');
            }
          }).catch((error) => {
            console.error('接口请求失败:', error);
            this.$message.error('请求失败，请稍后重试！');
          }).finally(() => {
            this.confirm_disabled = false; // 恢复按钮
          });
        } else {
          this.confirm_disabled = false; // 恢复按钮
          this.$message.warning('请填写完整的登录信息！');
        }
      });
    }
  }
};
</script>

<style scoped>
.loginBody {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: #83c8d1;
}
.loginDiv {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 450px;
  height: 330px;
  background: #fff;
  border-radius: 5%;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}
.login-title {
  margin: 20px 0;
  text-align: center;
  font-size: 24px;
  color: #333;
}
.login-content {
  width: 400px;
  margin: 0 auto;
}
</style>
